﻿#region using statements

using System;
using Microsoft.Xna.Framework;
using XNAGameEngine.Logs;

#endregion

namespace XNAGameEngine.GUI
{
    public sealed class ManagerGUI : DrawableGameComponent, IManagerGUI
    {
        #region local fields

        /// <summary>
        /// the registry to store and create the GUILayout objects
        /// </summary>
        private readonly GUILayoutRegistry _registry;

        /// <summary>
        /// Thr current layout loaded
        /// </summary>
        private GUILayout _currentGUI;

        #endregion

        #region constructors

        /// <summary>
        /// Register the game engine
        /// </summary>
        /// <param name="game">the game engine</param>
        public ManagerGUI(Game game)
            : base(game)
        {
            Game.Services.AddService(typeof(IManagerGUI), this);
            Game.Components.Add(this);
            _registry = new GUILayoutRegistry(this);
            ManagerLogs.Write("ManagerGUI : Created");
            
        }

        #endregion

        #region IManagerGUI Members

        /// <summary>
        /// Load a specific GUILayout that has been registered. Only one 
        /// GUILayout can be loaded at a time.
        /// </summary>
        /// <param name="name">the name of the the GUILayout to be loaded</param>
        public void LoadGUILayout(string name)
        {
            if (_currentGUI != null)
                UnloadGUILayout();

            _currentGUI = _registry.CreateObjectInstance(name);
            _currentGUI.Initialize();
        }

        /// <summary>
        /// unload a GUILayout that is currently loaded
        /// </summary>
        public void UnloadGUILayout()
        {
            Cleanup();
            _currentGUI = null;
        }

        /// <summary>
        /// Registers a GUILayout object with this manager and maps a string 
        /// to it
        ///  </summary>
        /// <param name="key">the string to map to the GUILayout</param>
        /// <param name="guiLayoutType">the GUILayout being mapped</param>
        public void RegisterGUILayout(string key, Type guiLayoutType)
        {
            _registry.RegisterGUILayout(key, guiLayoutType);
        }

        /// <summary>
        /// Returns the name of the current GUILayout
        /// </summary>
        /// <returns>the name of the GUILayout</returns>
        public string GetGUILayoutName()
        {
            return _currentGUI.Name;
        }

        #endregion

        #region local methods

        /// <summary>
        /// Initialize the manager
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
        }

        /// <summary>
        /// Cleanup the current GUILayout
        /// </summary>
        public void Cleanup()
        {
            if (_currentGUI != null)
                _currentGUI.Cleanup();
        }

        /// <summary>
        /// Update the manager
        /// </summary>
        /// <param name="gameTime">snapshot of game timing values</param>
        public override void Update(GameTime gameTime)
        {
            if (_currentGUI != null)
                _currentGUI.Update(gameTime);

            base.Update(gameTime);
        }

        /// <summary>
        /// Draw the manager
        /// </summary>
        /// <param name="gameTime">snapshot of game timing values</param>
        public override void Draw(GameTime gameTime)
        {
            if (_currentGUI != null)
                _currentGUI.Draw(gameTime);

            base.Draw(gameTime);
        }

        #endregion
    }
}